package com.rd.student.jiaji.class1.jinhongyu.day0807;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class DaLeTou {
    public static void main(String[] args) throws IOException {
        String s1 = zhongJiang();
        int a = 0;
        for (int i = 0; i < 100; i++) {
            File file = new File("d1.txt");
            OutputStream os = new FileOutputStream(file, true); //追加
            String s2 = suiJi();
            byte[] arr = s2.getBytes();
            os.write(arr);
            os.close();
            if (s2.equals(s1)){
                System.out.println("中奖号码为:"+ s1 +"随机号码为:"+ s2 +"中奖");
                a++;
            }else {
                System.out.println("中奖号码为:"+ s1 +"随机号码为:"+ s2 +"未中奖");
            }
        }
        File file = new File("d.txt");
        OutputStream os = new FileOutputStream(file);
        byte[] arr = s1.getBytes();
        os.write(arr);
        System.out.println("数据已写入文件。");
        os.close();

        File file1 = new File("num.txt");
        OutputStream os1 = new FileOutputStream(file1);
        String s2 = String.valueOf(a);
        byte[] arr1 = s2.getBytes();
        os1.write(arr1);
        os1.close();
        System.out.println("数据已写入文件。");
    }

    public static String zhongJiang(){
        Set<String> set1 = new HashSet<>();
        while (set1.size() < 5) {
            int random = (int) (Math.random() * 35);
            set1.add(String.valueOf(random));
        }
        Set<String> set2 = new HashSet<>();
        while (set2.size()<2){
            int random = (int)(Math.random()*12);
            set2.add(String.valueOf(random));
        }

        StringBuilder str1 = new StringBuilder();
        Iterator<String> it1 = set1.iterator();
        while (it1.hasNext()){
            str1.append(it1.next()+" ");
        }

        StringBuilder str2 = new StringBuilder();

        Iterator<String> it2 = set2.iterator();
        while (it2.hasNext()){
            str2.append(it2.next()+" ");
        }
        String s = str1+"|"+str2;
        return s;
    }

    public static String suiJi(){
        Set<String> set1 = new HashSet<>();
        while (set1.size() < 5) {
            int random = (int) (Math.random() * 35);
            set1.add(String.valueOf(random));
        }
        Set<String> set2 = new HashSet<>();
        while (set2.size()<2){
            int random = (int)(Math.random()*12);
            set2.add(String.valueOf(random));
        }

        StringBuilder str1 = new StringBuilder();
        Iterator<String> it1 = set1.iterator();
        while (it1.hasNext()){
            str1.append(it1.next()+" ");
        }

        StringBuilder str2 = new StringBuilder();

        Iterator<String> it2 = set2.iterator();
        while (it2.hasNext()){
            str2.append(it2.next()+" ");
        }
        String s = str1+"|"+str2;
        return s;
    }
}